<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      v-hasPermi="['barinfo:barinfo:create']"
      plain
      type="primary"
      @click="openForm('create')"
    >
      <Icon class="mr-5px" icon="ep:plus" />
      新增
    </el-button>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="deviceinfo" >
            <el-tab-pane label="杆件下的设备列表" name="deviceinfo">
              <deviceinfoList :gjbh="scope.row.gjbh" />
            </el-tab-pane> 
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="杆件编号" align="center" prop="gjbh" width="120px"/>
      <el-table-column label="杆件路口方向" align="center" prop="fx" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ROAD_DIRECTION" :value="scope.row.fx" />
        </template>
      </el-table-column>
      <el-table-column label="道路名称" align="center" prop="dlmc" width="320px"/>
      <el-table-column label="杆件名称" align="center" prop="gjmc"  width="320px"/> 
      <el-table-column label="所属部门" align="center" prop="ssbm" width="120px"/>

      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['barinfo:barinfo:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['barinfo:barinfo:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <barinfoForm ref="formRef" :dlbh="dlbh"  @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { intersectionApi } from '@/api/intersection/intersection'
import barinfoForm from '../../../barinfo/barinfo/barinfoForm.vue'
import { barinfoApi, barinfoVO } from '@/api/barinfo/barinfo'
import deviceinfoList from './deviceinfoList.vue'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  dlbh?: String // 道路编号（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await intersectionApi.getbarinfoListByDlbh(props.dlbh)
  } finally {
    loading.value = false
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await barinfoApi.deletebarinfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
/** 监听主表的关联字段的变化，加载对应的子表数据 */
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dlbh: undefined as unknown
})
watch(
  () => props.dlbh,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.dlbh = val
    handleQuery()
  },
  { immediate: true, deep: true }
)
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.dlbh) {
    message.error('请选择一个道路、路口')
    return
  }
  formRef.value.open(type, id, props.dlbh)
}



/** 初始化 **/
onMounted(() => {
  getList()
})
</script>